import sys
import traceback

from xjlibrary.all_step.BaseAllStep import GlobalControl
from xjlibrary.our_file_dir import BaseDir

curPath = BaseDir.get_file_dir_absolute(__file__)
print(curPath)
sys.path.insert(0, curPath)  # 插入

from Step1_down_subject import main as step1_main
from Step2_downsubject import main as step2_main
from Step3_downjournal import main as step3_main
from Step4_downissue import main as step4_main
from Step5_downCover import main as step5_main
from Step6_mapCover import main as step6_main

class UseGlobalControl(GlobalControl):

    def __init__(self):
        super().__init__(__file__, BaseDir.get_new_path(BaseDir.get_file_dir_absolute(__file__), "logs"))

    def imports(self):
        pass

    def run(self, strings):
        """
        该步骤运行代码
        :return:
        """
        try:
            if strings == "":
                return True
            result = eval(strings)
        except:
            print(traceback.format_exc())
            return False
        return result

    def start_hook(self, key):
        if key == "6":
            self.key = "6"
            self.nowstep = ""
            return True

    def one_check(self):
        """
        该步骤检查代码是否运行完成
        :return:
        """
        if self.key == "1":
            print("检查第一步")
            sql = "select stat from `subject` where stat=0"
            rows = self.mysqlutils.SelectFromDB(sql)
            if len(rows) == 58:
                print("执行正常")
                self.writetrue()
            else:
                self.writefalse("量不对{}".format(len(rows)))
        elif self.key == "2":
            print("检查第二步")

            sql = "select stat from `subject` where stat=0"
            rows = self.mysqlutils.SelectFromDB(sql)
            if len(rows) == 0:
                print("执行正常")
                self.writetrue()
            else:
                self.writefalse("量不对")

        elif self.key == "3":

            sql = "select jid,subjid from journal where stat = 0"
            rows = self.mysqlutils.SelectFromDB(sql)
            if len(rows) == 0:
                self.writetrue()
            else:
                self.writefalse("有journal没有下载 请检查原因")
        elif self.key == "4":

            sql = "select issueid  from issue where stat=0 and failcount<3"
            rows = self.mysqlutils.SelectFromDB(sql)
            if len(rows) == 0:
                self.writetrue()
            else:
                self.writefalse("有issue没有下载 请检查原因")
        elif self.key == "5":
            sql = "select jid,cover from journal where cover_stat = 0"
            rows = self.mysqlutils.SelectFromDB(sql)
            if len(rows) == 0:
                self.writetrue()
            else:
                self.writefalse("有图片没有下载")
        elif self.key == "6":
            self.writetrue("注意远程与本地图片合并总量变化然后生成txt文件")

        if "6" in self.jsondicts.keys() and self.jsondicts["6"]["isFinish"]:
            return True
        return False


if __name__ == "__main__":
    globalcontrol = UseGlobalControl()
    globalcontrol.work()
